package com.ws.shop.action.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Payment;
import com.ws.shop.service.PaymentService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/payment/*")
public class PaymentAction extends BaseAdminAction{

	private static Logger log = Logger.getLogger(PaymentAction.class);
	@Autowired
	private PaymentService paymentService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request , Model model){
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		try{
			Page page = paymentService.findPaymentListPage(pageno,0);
			model.addAttribute(Page.PAGE, page);
			model.addAttribute("stateDescs", Payment.STATE_DESCS);
			return "payment_index";
		}catch(Exception e){
			log.error("查询支付通道列表失败",e);
			model.addAttribute(ERROR_MSG,"系统异常，请稍后再试");
			return ERROR;
		}
		
	}
	
	public String update(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id <= 0){
			return this.ajaxJsonErrorMessage(response, "请选择要更新的支付通道");
		}
		String interfaces = RequestUtil.getStringParameter(request, "interfaces", null);
		String business = RequestUtil.getStringParameter(request, "business", null);
		String mail = RequestUtil.getStringParameter(request, "mail", null);
		String seckey = RequestUtil.getStringParameter(request, "seckey", null);
		int currency = RequestUtil.getIntParameter(request, "currency", 0);
		int fee = RequestUtil.getIntParameter(request, "fee", 0);
		int state = RequestUtil.getIntParameter(request, "state", 0);
		Payment payment = new Payment(id,interfaces,business,mail,seckey,currency,fee,state);
		try{
			paymentService.saveOrUpdate(payment);
			return this.ajaxJsonSuccessMessage(response, "操作成功");
		}catch(Exception e){
			log.error("更新支付通道失败:"+payment,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，请稍后再试");
		}
	}
	
}
